export default (Vue, options)=>{
	Vue.prototype.base = {
    fullTimeStr(time){
			const dt = new Date(time);
			if(!time){return ''};
			return `${dt.toLocaleDateString().replace(/\//g,'-')} ${dt.getHours()<10?'0':''}${dt.getHours()}:${dt.getMinutes()<10?'0':''}${dt.getMinutes()}:${dt.getSeconds()<10?'0':''}${dt.getSeconds()}`;
		},
		
		timeStr(time){
			const dt = new Date(time);
			if(!time){return ''};
			return `${dt.getHours()<10?'0':''}${dt.getHours()}:${dt.getMinutes()<10?'0':''}${dt.getMinutes()}:${dt.getSeconds()<10?'0':''}${dt.getSeconds()}`;
		},
		
		dateStr(time){
			const dt = new Date(time);
			if(!time){return ''};
			return dt.toLocaleDateString().replace(/\//g,'-');
		},
		
		fullDateStr(time){
			if(!time){return ''};
			const now = new Date(time);
			return now.getFullYear()+"-"+((now.getMonth()+1)<10?"0":"")+(now.getMonth()+1)+"-"+(now.getDate()<10?"0":"")+now.getDate();
		},
		
		throttle(fn,context,delay,val){
	        clearTimeout(fn.timeoutId);
	        fn.timeoutId = setTimeout(function(){
	            fn.call(context,val);
	        },delay);
	    },
	    checkPhone(val){
	    	//备注：初始化带出来的测试数据都会有X，但正式服务器上不会有
	    	let reg = /^1(3|4|5|7|8)\d{9}$/;
	    	if(!val){
	    		return '请填写联系方式！'
	    	}else{
	    		return reg.test(val)==true ? '' :'联系方式格式不正确！';
	    	}
	    	
	    },
	    checkTime(val1,val2){    //备注：val1：开始时间，val2 结束时间
	    	if(val2){
	    		let start = parseInt(val1.replace(/-/g,''));
	    		let end = parseInt(val2.replace(/-/g,''));
	    		if(start>end){
  					return '开始时间不能在结束时间之后！'
  				}else{
  					return '';
  				}
	    	}else{
	    		return '请选择结束时间'
	    	}
	    },
	    checkBeginTime(val){
			var startTime = new Date().getTime(); 
			var endTime = new Date(Date.parse(val.replace(/-/g, "/"))).getTime(); 
			var dates = parseInt(Math.abs((startTime - endTime))/(1000*60*60*24)); 
			return dates 
	    },
	    checkNumber(val){
	    	let reg =/^[\+\-]?[0-9]\d*(\.\d*)?$/
	    	return reg.test(val);
	    },
	    //res ： 数据源  forms ：要修改的对象
	    getInfo(res,forms){
			let a = Object.keys(res);
			a.forEach((item)=>{
				if(forms.hasOwnProperty(item)){
					forms[item]=res[item]=="null" ? '' : res[item];
				}
			})
		},
	    /*
	     * valids：验证规则
	     * forms：数据源
	     * 备注：valids和forms中字段名需要一样，并且通过msg string长度判断验证是否痛过
	     * */
	    myValidAct(valids, forms, cur){
    		let result = true;
    		const validsArry = Object.keys(valids);
    		validsArry.forEach((kw,kwi)=>{
    			if(arguments.length == 3){//单步验证
    				if(kwi <= validsArry.indexOf(cur)){ 
    					valids[kw].func(forms[kw]);
	    				if(valids[kw].msg.length>0){result = false};
    				}else{
    					if(valids[kw].msg.length>0){
    						result = false;
    					}else{
    						valids[kw].func(forms[kw]);
    						if(valids[kw].msg.length>0){result = false};
    						valids[kw].msg = '';
    					};
    				}
    			}else{
    				valids[kw].func(forms[kw]);
    				if(valids[kw].msg.length>0){result = false};
    			}
    		});
    		return result;
    	},
    	
    	clog(data){
    		console.log(JSON.parse(JSON.stringify(data)));
    	},
    	
    	money(val){
    		if (!val) return val;
    		let arry = parseFloat(val).toFixed(2).split('.');
    		return `${parseInt(arry[0]).toLocaleString()}.${arry[1]}`;
    	},
    	
    	/*
    	 * 提取需要的obj需要的字段，返回值为提取后的obj
    	 * target：目标obj
    	 * orgin：数据源
    	 * */
    	extractObj(target,orgin){
    		let data = {};
//  		function clone(obj) {
//			    if (null == obj || "object" != typeof obj) return obj;
//			    if (obj instanceof Date) {
//			       var copy = new Date();
//			        copy.setTime(obj.getTime());
//			        return copy;
//			    }
//			    if (obj instanceof Array | obj instanceof Object) {
//			        var copy = (obj instanceof Array)?[]:{};
//			        for (var attr in obj) {
//			            if (obj.hasOwnProperty(attr))
//			              copy[attr] = clone(obj[attr]);
//			        }
//			        return copy;
//			    }
//			}
    		Object.keys(target).forEach(item=>{
    			data[item] = orgin[item];
    		})
    		return Object.assign({},data);
    	},
    	
    	/*
    	 *获取日期相隔天数，格式：2017-10-11 
    	 * */
    	getDateDiff(start,  end){
    		if(!start||!end){return 0};
    		const sDate = start.split('-');
    		const eDate = end.split('-');
	        return parseInt(Math.abs(new  Date(sDate[0],parseInt(sDate[1])-1,sDate[2])  -  new  Date(eDate[0],parseInt(eDate[1])-1,eDate[2]))  /  1000  /  60  /  60  /24);    
	   	},
	   	
	   	arrayEquals(arr1,arr2){
	   		if (!arr1 || !arr2){return false;}
	   		if (arr1.length != arr2.length){return false;}
	   		for (var i = 0, l = arr1.length; i < l; i++) {
			    if (arr1[i] instanceof Array && arr2[i] instanceof Array) {
			      if (!this.arrayEquals(arr1[i],arr2[i]))
			        return false;    
			    }      
			    else if (arr1[i] != arr2[i]) { 
			      return false;  
			    }      
				}    
				return true;
	   	},
	   	
	   	beforeToday(val){
	   		const now = new Date();
				let today = parseInt(now.getFullYear()+((now.getMonth()+1)<10?"0":"")+(now.getMonth()+1)+(now.getDate()<10?"0":"")+now.getDate());
	   		const cur = parseInt(val.replace(/-/g, ""));
	   		return today >= cur;
	   	},
	   	
	   	cutmoney(val){
    		if (!val) return val;
    		return parseFloat(val).toFixed(2);
    	},
		
			findById(id,data,key){
				let obj = null;
				data.forEach(item=>{
					if(item[key]==id){obj = item};
				})
				return obj?obj:{};
			},
			
  		deepClone(obj) {
			    if (null == obj || "object" != typeof obj) return obj;
			    if (obj instanceof Date) {
			       var copy = new Date();
			        copy.setTime(obj.getTime());
			        return copy;
			    }
			    if (obj instanceof Array | obj instanceof Object) {
			        var copy = (obj instanceof Array)?[]:{};
			        for (var attr in obj) {
			            if (obj.hasOwnProperty(attr))
			              copy[attr] = this.deepClone(obj[attr]);
			        }
			        return copy;
			    }
			},
			
			decimalCompare(a,b){
				return parseInt(String(a).replace('.','')) > parseInt(String(b).replace('.',''));
			},
  		
  		
		
	}
};